package java_thinking.unit_21._21_4;

import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

/**
 * @Desc:   4-11    线程安全且可变的Point类
 *                  构造出了一个发布其底层可变状态的车辆追踪器
 * @author: cww
 * @DateTime: 2019/6/3 9:32
 */
@ThreadSafe
public class SafePoint {
    @GuardedBy("this") private int x, y;

    public SafePoint(int[] a) {
        this(a[0], a[1]);
    }

    public SafePoint(SafePoint p) {
        this(p.get());
    }
    public SafePoint(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public synchronized int[] get() {
        return new int[] {x, y};
    }

    public synchronized void set(int x, int y) {
        this.x = x; this.y = y;
    }
}


class PointTest {
    public static void main(String[] args) {
        SafePoint safePoint = new SafePoint(new int[]{2, 3});
        int[] a = (safePoint.get());
        System.out.println(safePoint.get());
        System.out.println(a[0]);
        safePoint.set(8, 9);
        System.out.println(safePoint.get()[0]);

    }
}